// controllers/subject.go
package controllers

import (
	"myapi/models"
	"myapi/repositories"
	"myapi/utils"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// SubjectController 处理科目相关请求
type SubjectController struct {
	repo repositories.SubjectRepository
}

// NewSubjectController 创建科目控制器实例
func NewSubjectController() *SubjectController {
	return &SubjectController{
		repo: repositories.SubjectRepository{},
	}
}

// CreateSubject 创建新科目
func (c *SubjectController) CreateSubject(ctx *gin.Context) {
	var subject models.Subject
	if err := ctx.ShouldBindJSON(&subject); err != nil {
		utils.ErrorResponse(ctx, http.StatusBadRequest, "Invalid request data")
		return
	}

	if err := c.repo.Create(&subject); err != nil {
		utils.ErrorResponse(ctx, http.StatusInternalServerError, "创建失败"+err.Error())
		return
	}

	utils.SuccessResponse(ctx, subject, "科目创建成功")
}

// GetSubject 获取单个科目
func (c *SubjectController) GetSubject(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusBadRequest, "请求失败，科目ID错误")
		return
	}

	subject, err := c.repo.FindByID(uint(id))
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusNotFound, "请求失败，科目不存在")
		return
	}

	utils.SuccessResponse(ctx, subject, "请求成功，科目获取成功")
}

// GetSubjects 获取所有科目
func (c *SubjectController) GetSubjects(ctx *gin.Context) {
	pageStr := ctx.DefaultQuery("page", "1")
	pageSizeStr := ctx.DefaultQuery("page_size", "10")

	page, _ := strconv.Atoi(pageStr)
	pageSize, _ := strconv.Atoi(pageSizeStr)

	if page < 1 {
		page = 1
	}
	if pageSize < 1 || pageSize > 100 {
		pageSize = 10
	}

	subjects, count, err := c.repo.FindAll(page, pageSize)
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusInternalServerError, "请求失败，科目获取失败"+err.Error())
		return
	}

	data := gin.H{
		"subjects":   subjects,
		"total":      count,
		"page":       page,
		"page_size":  pageSize,
		"total_page": (count + int64(pageSize) - 1) / int64(pageSize),
	}

	utils.SuccessResponse(ctx, data, "请求成功，科目获取成功")
}

// UpdateSubject 更新科目
func (c *SubjectController) UpdateSubject(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusBadRequest, "请求失败，科目ID错误")
		return
	}

	subject, err := c.repo.FindByID(uint(id))
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusNotFound, "请求失败，科目不存在")
		return
	}

	var input struct {
		Name        string `json:"name"`
		Description string `json:"description"`
	}

	if err := ctx.ShouldBindJSON(&input); err != nil {
		utils.ErrorResponse(ctx, http.StatusBadRequest, "请求失败，请求数据错误")
		return
	}

	// 更新字段
	if input.Name != "" {
		subject.Name = input.Name
	}
	if input.Description != "" {
		subject.Description = input.Description
	}

	if err := c.repo.Update(subject); err != nil {
		utils.ErrorResponse(ctx, http.StatusInternalServerError, "请求失败，科目名称重复了"+err.Error())
		return
	}

	utils.SuccessResponse(ctx, subject, "请求成功，科目更新成功")
}

// DeleteSubject 删除科目
func (c *SubjectController) DeleteSubject(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		utils.ErrorResponse(ctx, http.StatusBadRequest, "请求失败，科目ID错误")
		return
	}

	if err := c.repo.Delete(uint(id)); err != nil {
		utils.ErrorResponse(ctx, http.StatusInternalServerError, "请求失败，科目删除失败"+err.Error())
		return
	}

	utils.SuccessResponse(ctx, nil, "请求成功，科目删除成功")
}
